package com.study.contoller;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.study.entity.User;
import com.study.utils.JsonUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

//@RequestController 等于 下面的所有方法都加上@ResponseBody，不再走视图解析器
// @RequestController配合@Controller使用
@Controller
public class UserController {

//    @ResponseBody 加上后就不会走视图解析器，直接返回你要返回的值
   // produces解决乱码问题 produces = "application/json;charset=utf-8"),在xml中加入json乱码配置就可以删掉produces了
    @RequestMapping(value = "/j1")
    @ResponseBody
    public String json1() throws JsonProcessingException {
        // jackson包下有一个类叫 ObjectMapper
        ObjectMapper mapper = new ObjectMapper();

        // 创建一个对象
        User user = new User("任晓龙","男",22);

        // 可以把一个值变成字符串
        String str = mapper.writeValueAsString(user);

        return str;
    }

    @RequestMapping(value = "/j2")
    @ResponseBody
    public String json2() throws JsonProcessingException {
        // jackson包下有一个类叫 ObjectMapper
        ObjectMapper mapper = new ObjectMapper();

        List<User> userList = new ArrayList<>();
        // 创建一个对象
        User user1 = new User("任晓龙1","男",22);
        User user2 = new User("任晓龙2","男",22);
        User user3 = new User("任晓龙3","男",22);

        userList.add(user1);
        userList.add(user2);
        userList.add(user3);

        // 可以把一个值变成字符串
        String str = mapper.writeValueAsString(userList);

        return str;
    }

    @RequestMapping(value = "/j3")
    @ResponseBody
    public String json3() throws JsonProcessingException {
        // jackson包下有一个类叫 ObjectMapper
        ObjectMapper mapper = new ObjectMapper();

        // 创建一个时间对象
        Date date = new Date();
        // 方法一：自定义日期格式
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

//        方法二：使用ObjectMapper格式化输出时间
        mapper.configure(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS,false);
        // ObjectMapper，时间解析后的默认格式为：Timestamp,时间戳
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        mapper.setDateFormat(sdf);
        //  mapper.writeValueAsString可以把一个值变成字符串
        return mapper.writeValueAsString(date);
    }

    @RequestMapping(value = "/j4")
    @ResponseBody
    public String json4() throws JsonProcessingException {
        // 调用工具类
        Date date = new Date();
        return JsonUtil.getJson(date,"yyyy-MM-dd HH:mm:ss");
    }

    @RequestMapping(value = "/j5")
    @ResponseBody
    public String json5() throws JsonProcessingException {
        // jackson包下有一个类叫 ObjectMapper
        ObjectMapper mapper = new ObjectMapper();

        List<User> userList = new ArrayList<>();
        // 创建一个对象
        User user1 = new User("任晓龙1","男",22);
        User user2 = new User("任晓龙2","男",22);
        User user3 = new User("任晓龙3","男",22);

        userList.add(user1);
        userList.add(user2);
        userList.add(user3);
        return JsonUtil.getJson(userList);
    }

    // FastJson
    @RequestMapping(value = "/j6")
    @ResponseBody
    public String json6() throws JsonProcessingException {
        List<User> userList = new ArrayList<>();
        // 创建一个对象
        User user1 = new User("任晓龙1","男",22);
        User user2 = new User("任晓龙2","男",22);
        User user3 = new User("任晓龙3","男",22);

        userList.add(user1);
        userList.add(user2);
        userList.add(user3);

        String str  = JSON.toJSONString(userList);
        return str;
    }
}
